package com.szqhyxj.youxiangju.biz.onlineChannel.onlineReceipt;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.onlineChannel.ChannelModel;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by leung on 2018/12/25.
 */

public class OnlineReceiptActivity extends BaseActivity implements OnlineReceiptContract.View, NavigationBar.NavigationBarListener {

    ChannelModel.RowsItem rows;
    private final static String CHANNELID = "channelId";
    private final static String BANKNAME = "bankName";
    private final static String CARDNO = "cardNo";
    private final static String MONEY = "money";
    private final static String MINAMOUNT = "minamount";
    private final static String MAXAMOUNT = "maxamount";
    private final static String OUTCARDID ="outCardId";
    private final static String INCARDID = "inCardId";


    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.amount)
    EditText amount;
    @BindView(R.id.handlingfee)
    TextView handlingfee;
    @BindView(R.id.billingcard)
    TextView billingcard;
    @BindView(R.id.startReceipt)
    TextView startReceipt;
    @BindView(R.id.amountrange)
    TextView amountrange;
    @BindView(R.id.fee)
    TextView fee;

    private String bankname;
    private String cardno;
    private String money;
    private int channelId;
    private int consumeType;
    private int minamount;
    private int maxamount;
    private int outCardId;
    private int inCardId;
    OnlineReceiptPresenter presenter;

    public static void start(Activity activity, String bankName, String cardno, String money, int channelId,int outCardId,int inCardId, int minamount, int maxamount) {
        Intent intent = new Intent();
        intent.setClass(activity, OnlineReceiptActivity.class);
        intent.putExtra(BANKNAME, bankName);
        intent.putExtra(CARDNO, cardno);
        intent.putExtra(MONEY, money);
        intent.putExtra(CHANNELID, channelId);
        intent.putExtra(OUTCARDID,outCardId);
        intent.putExtra(INCARDID,inCardId);
        intent.putExtra(MINAMOUNT, minamount);
        intent.putExtra(MAXAMOUNT, maxamount);
        activity.startActivity(intent);

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receipt_detail);
        ButterKnife.bind(this);
        navigationBar.setNavigationBarListener(this);
        bankname = getIntent().getStringExtra(BANKNAME);
        cardno = getIntent().getStringExtra(CARDNO);
        money = getIntent().getStringExtra(MONEY);
        channelId = getIntent().getIntExtra(CHANNELID, 0);
        outCardId = getIntent().getIntExtra(OUTCARDID,0);
        inCardId = getIntent().getIntExtra(INCARDID,0);
        cardno = getIntent().getStringExtra(CARDNO);
        String bankcardnosub = cardno.substring(cardno.length() - 4, cardno.length());
        String str = "(" + bankcardnosub + ")";
        String bankname = getIntent().getStringExtra(BANKNAME);
        navigationBar.setNaviTitle(bankname + str);
        minamount = getIntent().getIntExtra(MINAMOUNT, 0);
        maxamount = getIntent().getIntExtra(MAXAMOUNT, 0);
        presenter = new OnlineReceiptPresenter(this);
        //presenter.getdeposit();
        amount.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0, s.toString().indexOf(".") + 3);
                        amount.setText(s);
                        amount.setSelection(s.length());
                    }
                } //如果.在起始位置,则起始位置自动补0
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    amount.setText(s);
                    amount.setSelection(2);
                }
                //如果起始位置为0并且第二位跟的不是".",则无法后续输入
                if (s.toString().startsWith("0") && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        amount.setText(s.subSequence(0, 1));
                        amount.setSelection(1);
                        return;
                    }
                }

                presenter.setAmount(s.toString(), money, minamount, maxamount,"0.0065");
            }
        });

    }

    public abstract static class SimpleTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }


    public void showContent(String fees, int minamount, int maxamount) {
        fee.setText("费率:" + fees + "%+单笔" + "0" + "元");
        amountrange.setText("单笔最低" + minamount + "元,单笔最高" + maxamount + "元");
    }

    @Override
    public void showCard(String cardno) {
        billingcard.setText(cardno);
    }

    @Override
    public void showHandingFee(String fee) {
        handlingfee.setText(fee);
    }

    @Override
    public void showEnClickable() {
        startReceipt.setEnabled(false);
        startReceipt.setBackgroundResource(R.drawable.background_btn_grey04);
    }

    @Override
    public void showClickable() {
        startReceipt.setEnabled(true);
        startReceipt.setBackgroundResource(R.drawable.background_btn04);
    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {

    }
}
